import pygame

from shared_variables import CONFIG


class ProjectDialog:
    def __init__(self, screen, screen_size, callback):
        self.screen = screen
        # 调整对话框大小
        self.dialog_rect = pygame.Rect(screen_size[0] // 8, screen_size[1] // 8, 3 * screen_size[0] // 4,
                                       3 * screen_size[1] // 4)
        self.dialog_surface = pygame.Surface(self.dialog_rect.size, pygame.SRCALPHA)  # 支持透明度
        self.dialog_surface.fill((0, 0, 0, 128))  # 设置对话框为半透明

        # 图像路径
        self.arrange_image_path = CONFIG['assets']['project']['tittle']
        self.settle_image_path = CONFIG['assets']['project']['done']
        self.reconsider_image_path = CONFIG['assets']['project']['reconsider']
        self.callback = callback

        # 按钮大小
        self.button_width = 100
        self.button_height = 80

        # 调整按钮位置
        self.settle_button_rect = pygame.Rect(self.dialog_rect.width // 2 + 50,
                                              self.dialog_rect.height - self.button_height+100,
                                              self.button_width, self.button_height)
        self.reconsider_button_rect = pygame.Rect(self.dialog_rect.width // 2 + 250,
                                                  self.dialog_rect.height - self.button_height+100,
                                                  self.button_width, self.button_height)

        self.settle_clicked = False
        self.reconsider_clicked = False

    def draw(self, image_path):
        # 加载背景图片
        background_image = pygame.image.load(image_path)

        # 调整背景图片大小，使其不改变长宽比
        bg_img_rect = background_image.get_rect(center=(self.dialog_rect.width // 2, self.dialog_rect.height // 2))
        background_image_scaled = pygame.transform.scale(background_image,
                                                         (bg_img_rect.width - 200, bg_img_rect.height - 300))
        new_image_rect = background_image_scaled.get_rect(
            center=(self.dialog_rect.width // 2, self.dialog_rect.height // 2))
        # 将背景图片绘制到dialog_surface上
        self.dialog_surface.blit(background_image_scaled, new_image_rect.topleft)

        # 加载其他图像
        arrange_image = pygame.image.load(self.arrange_image_path)
        settle_image = pygame.image.load(self.settle_image_path)
        reconsider_image = pygame.image.load(self.reconsider_image_path)

        # 绘制图像
        self.dialog_surface.blit(arrange_image, (self.dialog_rect.width // 2 - arrange_image.get_width() // 2, 20))

        self.dialog_surface.blit(settle_image, (
            self.dialog_rect.width // 2 - self.button_width, self.dialog_rect.height - self.button_height))
        self.dialog_surface.blit(reconsider_image, (
            self.dialog_rect.width // 2 + self.button_width, self.dialog_rect.height - self.button_height))

        # 在屏幕上绘制dialog_surface
        self.screen.blit(self.dialog_surface, (self.dialog_rect.x, self.dialog_rect.y))

    def handle_event(self, mouse_pos):
        if self.settle_button_rect.collidepoint(mouse_pos):
            self.settle_clicked = True
            self.reconsider_clicked = False

        if self.reconsider_button_rect.collidepoint(mouse_pos):
            self.reconsider_clicked = True
            self.settle_clicked = False

        if self.settle_clicked:
            self.settle_clicked = False
            return "ai_talk"

        if self.reconsider_clicked:
            self.reconsider_clicked = False
            self.callback()
